package school30.io;

import java.util.Vector;

public class File {

    public Vector path = new Vector();

    public File(String path) {
        int i = -1;
        while ((i = path.indexOf("/")) >= 0) {
            String d = path.substring(0, i);
            path = path.substring(i + 1);
            if (d.length() > 0) {
                this.path.addElement(d);
            }
        }
        if (path.length() > 0)
            this.path.addElement(path);
    }
    
    public File(Vector v) {
        this.path = v;
    }

    public String name()
    {
        if (isRoot())
            return "/";
        return (String) path.lastElement();
    }

    public String path()
    {
        String p = "";
        for (int i = 0; i < path.size(); i++)
        {
            p += "/" + path.elementAt(i);
        }
        if (p.length() == 0)
            return "/";
        return p;
    }

    public File getParent() {
         Vector v = new Vector(path.size() - 1);
         for (int i = 0; i < path.size() - 1; i++)
             v.addElement(path.elementAt(i));
         return new File(v);
    }

    public File getChild(String name) {
         Vector v = new Vector(path.size() + 1);
         for (int i = 0; i < path.size(); i++)
             v.addElement(path.elementAt(i));
         v.addElement(name);
         return new File(v);
    }

    public File removeFirst() {
         Vector v = new Vector(path.size() - 1);
         for (int i = 1; i < path.size(); i++)
             v.addElement(path.elementAt(i));
         return new File(v);
    }

    public int pathLength() {
         return path.size();
    }

    public boolean isRoot()
    {
        return path.isEmpty();
    }

    public boolean isSubRoot()
    {
        return pathLength() == 1;
    }
}
